package edu.vt.ece_3574.amber_alert.curfew;

import com.google.android.maps.GeoPoint;

import edu.vt.ece_3574.amber_alert.accounts.Account;
import edu.vt.ece_3574.amber_alert.database.DatabaseHandler;

/**
 * This class handles determining whether the member has met their curfew
 * 
 * @author acorvin
 */
public class CurfewMessageDisplayer {
	private static CurfewMessageDisplayer curfewMessageDisplayer = new CurfewMessageDisplayer();
	
	/**
	 * Returns the singleton static CurfewMessageDisplayer
	 * @return the CurfewMessageDisplayer
	 */
	public static CurfewMessageDisplayer get() {
		return curfewMessageDisplayer;
	}
	
	public String getToastMessage(Account account) {
		//Return the appropriate string based on whether the member is at the specified location
		if (checkLocation(account)) {
			return "The member named " + account.getName() + " has met their curfew.";
		}
		else {
			return "The member named " + account.getName() + " did not meet their curfew.";
		}
	}
	
	/**
	 * Determines if the member is at the specified location
	 * @return boolean of whether the member is where they should be
	 */
	private boolean checkLocation(Account account) {
		//Get the selected member's last known location
		GeoPoint lastKnownLocation = DatabaseHandler.get().getLocationFromServer(account);
		//Get the location that was set for the curfew of the selected member
		GeoPoint curfewLocation = account.getCurfewLocation();
		
		//Determine whether the member's location is within the tolerance compared to the curfew location
		int locationTolerance = 500;
		boolean longitudeWithinTolerance = checkWithinTolerance(lastKnownLocation.getLongitudeE6(), curfewLocation.getLongitudeE6(), locationTolerance);
		boolean latitudeWithinTolerance = checkWithinTolerance(lastKnownLocation.getLatitudeE6(), curfewLocation.getLatitudeE6(), locationTolerance);
		
		//Return the boolean combination of whether the latitude and longitude are within the tolerance
		return (longitudeWithinTolerance && latitudeWithinTolerance);
	}
	
	/**
	 * Determines whether the two numbers are within the specified tolerance
	 * @param firstNum the first number
	 * @param secondNum the second number
	 * @param tolerance the allowed tolerance
	 * @return whether the difference between the numbers is within the tolerance
	 */
	private boolean checkWithinTolerance(int firstNum, int secondNum, int tolerance) {
		//Return whether the absolute value of the difference between the two numbers is less than the tolerance
		return (Math.abs(firstNum - secondNum) < tolerance);
	}
}
